package com.solver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.solver.constant.MessageConstant;
import com.solver.constant.PasswordConstant;
import com.solver.constant.StatusConstant;
import com.solver.context.BaseContext;
import com.solver.dto.TeacherDTO;
import com.solver.dto.TeacherLoginDTO;
import com.solver.dto.TeacherPageQueryDTO;
import com.solver.entity.Teacher;
import com.solver.exception.AccountLockedException;
import com.solver.exception.AccountNotFoundException;
import com.solver.exception.PasswordErrorException;
import com.solver.mapper.TeacherMapper;
import com.solver.result.PageResult;
import com.solver.service.TeacherService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 员工登录
     *
     * @param teacherLoginDTO
     * @return
     */
    public Teacher login(TeacherLoginDTO teacherLoginDTO) {
        String username = teacherLoginDTO.getUsername();
        String password = teacherLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Teacher teacher = teacherMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (teacher == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对前端的传回的密码进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(teacher.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (teacher.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return teacher;
    }

    @Override
    public void save(TeacherDTO teacherDTO) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDTO, teacher);
        teacher.setStatus(StatusConstant.ENABLE);
//        teacher.setCreateTime(LocalDateTime.now());
//        teacher.setUpdateTime(LocalDateTime.now());
        teacher.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

//
//        teacher.setCreateUser(BaseContext.getCurrentId());
//        teacher.setUpdateUser(BaseContext.getCurrentId());

        teacherMapper.insert(teacher);
    }

    @Override
    public PageResult pagequery(TeacherPageQueryDTO teacherPageQueryDTO) {

        PageHelper.startPage(teacherPageQueryDTO.getPage(),teacherPageQueryDTO.getPageSize());
        Page<Teacher> page  = teacherMapper.pageQuery(teacherPageQueryDTO);
        long total = page.getTotal();
        List<Teacher> records = page.getResult();

        return new PageResult(total,records);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Teacher teacher = Teacher.builder()
                .id(id)
                .status(status)
                .build();
        teacherMapper.update(teacher);
    }

    @Override
    public Teacher getById(Long id) {
        Teacher teacher  = teacherMapper.getById(id);
        return teacher;
    }

    @Override
    public void update(TeacherDTO teacherDTO) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDTO,teacher);//对象属性拷贝
//        teacher.setUpdateTime(LocalDateTime.now());
//        teacher.setUpdateUser(BaseContext.getCurrentId());//获取当前id
        teacherMapper.update(teacher);
    }

}
